package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import net.qhzw.vip.qiyu.entity.ProcessNotice;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.ProcessNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("process/notice")
@Api(tags = "维修紧急处理人")
public class ProcessNoticeController {

    @Autowired
    private ProcessNoticeService processNoticeService;

    /**
     * 分页查询维修紧急处理人
     * @param page
     * @param name
     * @return
     */
    @GetMapping
    public JsonResult findByPage(Page<ProcessNotice> page, String name){
        Page<ProcessNotice> noticePage = processNoticeService.page(page, new LambdaQueryWrapper<ProcessNotice>()
                .like(StringUtils.isEmpty(name), ProcessNotice::getName, name)
                .last("ORDER BY created"));
        return JsonResult.ok(noticePage);
    }


    /**
     * 根据id查询维修紧急处理人
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public JsonResult findById(@PathVariable Long id){
        ProcessNotice processNotice = processNoticeService.getById(id);
        return JsonResult.ok(processNotice);
    }


    /**
     * 新增维修紧急处理人
     * @param processNotice
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody ProcessNotice processNotice){
        int emailCount = processNoticeService.count(new LambdaQueryWrapper<ProcessNotice>()
                .eq(ProcessNotice::getEmail, processNotice.getEmail())
                .eq(ProcessNotice::getStatus, 1));
        if (emailCount > 0){
            return JsonResult.fail( processNotice.getEmail() + " 该邮箱已存在，无需重复添加！");
        }
        int nameCount = processNoticeService.count(new LambdaQueryWrapper<ProcessNotice>()
                .eq(ProcessNotice::getName, processNotice.getName())
                .eq(ProcessNotice::getStatus, 1));
        if (nameCount > 0){
            return JsonResult.fail( processNotice.getName() + " 该人员已存在，无需重复添加！");
        }
        processNotice.setId(IdWorker.getId());
        processNotice.setStatus(1);
        boolean isOk = processNoticeService.save(processNotice);
        if (isOk){
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    /**
     * 修改维修紧急处理人
     * @param processNotice
     * @return
     */
    @PutMapping
    public JsonResult update(@RequestBody ProcessNotice processNotice){
        int emailCount = processNoticeService.count(new LambdaQueryWrapper<ProcessNotice>()
                .eq(ProcessNotice::getEmail, processNotice.getEmail())
                .eq(ProcessNotice::getStatus, 1));
        if (emailCount > 1){
            return JsonResult.fail( processNotice.getEmail() + " 该邮箱已存在，无需重复添加！");
        }
        int nameCount = processNoticeService.count(new LambdaQueryWrapper<ProcessNotice>()
                .eq(ProcessNotice::getName, processNotice.getName())
                .eq(ProcessNotice::getStatus, 1));
        if (nameCount > 1){
            return JsonResult.fail( processNotice.getName() + " 该人员已存在，无需重复添加！");
        }
        processNotice.setCreated(new Date());
        boolean isOk = processNoticeService.updateById(processNotice);
        if (isOk){
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    /**
     * 修改维修紧急处理人状态
     * @param processNotice
     * @return
     */
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody ProcessNotice processNotice){
        processNotice.setCreated(new Date());
        boolean isOk = processNoticeService.updateById(processNotice);
        if (isOk){
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    /**
     * 删除维修紧急处理人
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public JsonResult setStatus(@PathVariable Long id){
        boolean isOk = processNoticeService.removeById(id);
        if (isOk){
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }




}
